package cn.walls1717.ggkt.vod.service.impl;

import cn.walls1717.ggkt.model.vod.Subject;
import cn.walls1717.ggkt.vo.vod.SubjectEeVo;
import cn.walls1717.ggkt.vod.listener.SubjectListener;
import cn.walls1717.ggkt.vod.mapper.SubjectMapper;
import cn.walls1717.ggkt.vod.service.SubjectService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 课程科目服务实现类
 *
 * @author walls1717
 * @since 2022/07/14
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Autowired
    private SubjectListener subjectListener;

    /**
     * 查询下一层的课程分类
     */
    @Override
    public List<Subject> getChildSubjectById(Long id) {
        LambdaQueryWrapper<Subject> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Subject::getParentId, id);
        // 根据parentId拿到数据集合
        List<Subject> subjectList = this.list(lqw);
        // 判断当前数据是否有下一级，如果有下一级，将HasChildren置为true
        subjectList = subjectList.stream().peek(item -> {
            Long pid = item.getId();
            Boolean flag = isChildren(pid);
            item.setHasChildren(flag);
        }).collect(Collectors.toList());
        return subjectList;
    }

    /**
     * 课程分类导出
     */
    @Override
    public void exportData(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("课程分类", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
            List<Subject> dictList = baseMapper.selectList(null);
            List<SubjectEeVo> dictVoList = dictList.stream().map(item -> {
                SubjectEeVo subjectEeVo = new SubjectEeVo();
                BeanUtils.copyProperties(item, subjectEeVo);
                return subjectEeVo;
            }).collect(Collectors.toList());
            EasyExcel.write(response.getOutputStream(), SubjectEeVo.class).sheet("课程分类").doWrite(dictVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 课程分类导入
     */
    @Override
    public void importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), SubjectEeVo.class, subjectListener)
                    .sheet()
                    .doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断是否有下一级
     *
     * @param pid 父id
     * @return true：有下一级 false：没有下一级
     */
    private Boolean isChildren(Long pid) {
        LambdaQueryWrapper<Subject> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Subject::getParentId, pid);
        int count = this.count(lqw);
        return count > 0;
    }
}
